﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KBC_Quiz_Show.Object;
using KBC_Quiz_Show.DBConnect;
using System.Data.SqlClient;
using System.Data;

namespace KBC_Quiz_Show.Bussiness
{
    class PlayerBus
    {
        Utils con;
        SqlConnection conn;
        SqlCommand cmd;
        DataSet ds;
        SqlDataAdapter da;
        public PlayerBus()
        {
            con = new Utils();
        }
        public void insertPlayer(Player p)
        {
            conn = con.getConnection();
            cmd = new SqlCommand("insertPlayer", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@pname", SqlDbType.VarChar).Value = p.PName;
            cmd.Parameters.Add("@paddress", SqlDbType.VarChar).Value = p.PAddress;
            cmd.Parameters.Add("@gender", SqlDbType.Int).Value = p.Gender;
            cmd.Parameters.Add("@age", SqlDbType.Int).Value = p.Age;
            cmd.Parameters.Add("@score", SqlDbType.Int).Value = p.Score;
            try
            {
                conn.Open();
                int i = cmd.ExecuteNonQuery();
               
            }
            catch (Exception e)
            {
                e.GetBaseException();
            }
            finally
            {
                conn.Close();
            }
        }

        public DataTable getHighScore()
        {
            //conn = con.getConnection();
            //cmd = new SqlCommand("selectHighScore", conn);
            //cmd.CommandType = CommandType.StoredProcedure;
            //da = new SqlDataAdapter(cmd);
            //ds = new DataSet();
            //da.Fill(ds, "HighScore");
            //return ds.Tables[0];
            conn = con.getConnection();
            string sql = "select top 10 * from Player order by score desc";
            cmd = new SqlCommand(sql, conn);
            da = new SqlDataAdapter(cmd);
            ds = new DataSet();
            da.Fill(ds, "HighScore");
            return ds.Tables[0];
        }
    }

}
